package com.frame.work.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frame.work.mapper.TblSensirionLogMapper;
import com.frame.work.entity.TblSensirionLog;
import com.frame.work.service.TblSensirionLogService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wjming
 */
@Service
@Slf4j
public class TblSensirionLogServiceImpl extends ServiceImpl<TblSensirionLogMapper, TblSensirionLog> implements TblSensirionLogService {
    @Autowired
    private TblSensirionLogMapper sensirionLogMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void subTableSaving(TblSensirionLog sensirionLog) {
        try {
            int b = sensirionLogMapper.insert(sensirionLog);
            if (b < 1) {
                log.warn("[SensirionLogSave]->[{}]", "保存security日志失败");
            }
        } catch (Exception e) {
            redisTemplate.delete("sensirionLog_" + sensirionLog.getDeviceMac());
            log.error("[SensirionLogSave]->[Exception][保存security日志失败 -> e:{}]", e.getMessage());
        }
    }

    @Override
    public List<TblSensirionLog> selectByTime(Integer pageNum, Integer pageSize, String gatewayMac, String deviceMac, String start, String end) {
        List<TblSensirionLog> tblSecurityLogs = this.sensirionLogMapper.selectByTime(pageNum, pageSize, gatewayMac, deviceMac, start, end);
        System.out.println(tblSecurityLogs);

        return tblSecurityLogs;
    }

    @Override
    public List<TblSensirionLog> selectByToday(String gatewayMac, String deviceMac) {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // new Date()为获取当前系统时间
        String format = df.format(new Date());
        System.out.println(df.format(new Date()));
        String start = format + " 00:00:00";
        String end = format + " 23:59:59";

        List<TblSensirionLog> tblSecurityLogs = this.sensirionLogMapper.selectByToday(gatewayMac, deviceMac, start, end);
        System.out.println(tblSecurityLogs);

        return tblSecurityLogs;
    }

    @Override
    public List<TblSensirionLog> selectByDay(String gatewayMac, String deviceMac, long time) {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // new Date()为获取当前系统时间
        String format = df.format(new Date(time));
        String start = format + " 00:00:00";
        String end = format + " 23:59:59";

        List<TblSensirionLog> tblSecurityLogs = this.sensirionLogMapper.selectByToday(gatewayMac, deviceMac, start, end);

        return tblSecurityLogs;
    }
}
